import axios from "axios";
import { Toast } from 'vant';
let service = axios.create({
    timeout: 10 * 1000,
    baseURL: process.env.VUE_APP_BASE_API,
})

service.interceptors.request.use(
    (config) => {
        config.headers["Authorization"] = "Bearer" + " " + localStorage.getItem("token");
        return config
    },
    (error) => {//请求发出前，发生错误的回调函数
        return Promise.reject(error)
    }
);

service.interceptors.response.use(
    (res) => {
        return res;
    },
    (error) => {//请求返回前发生错误的回调函数
        if (error.response.status == 401) {
            console.log("身份认证过期，请重新登录");
            // $router.push('/login');//跳转登录页
        } else if (error.response.status == 404) {
            Toast.fail("访问路径有误！")
        } else if (error.response.status == 500) {
            Toast.fail("服务器内部错误！")
        }
        return Promise.reject(error)
    }
)

export default service